package short_semester.questionnaire_planet.MyUtil;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import short_semester.questionnaire_planet.dao.*;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.AnalyseController.*;
import short_semester.questionnaire_planet.service.analyse.QuestionnaireRecordService;
import short_semester.questionnaire_planet.service.editService.QuestionService;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;


@Component
public class ExcelUtil {
    
    @Autowired
    QuestionnaireRecordService questionnaireRecordService;
    
    @Autowired
    QuestionnaireDao questionnaireDao;
    
    @Autowired
    QuestionService questionService;
    
    @Autowired
    QuestionnaireRecordDao questionnaireRecordDao;

    @Autowired
    SingleChoiceQuestionRecordDao singleChoiceQuestionRecordDao;
    @Autowired
    MultipleChoiceQuestionRecordDao multipleChoiceQuestionRecordDao;
//    @Autowired
//    MultiChoiceOptionRecordDao multiChoiceOptionRecordDao;
    @Autowired
    FillBlankQuestionRecordDao fillBlankQuestionRecordDao;
    @Autowired
    ScoreQuestionRecordDao scoreQuestionRecordDao;
    
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    public Workbook QuestionnaireRecordToExcel(Questionnaire questionnaire){
        
        Workbook book = new SXSSFWorkbook();
        Sheet sheet = book.createSheet();
        //写表头
        writeQuestionnaireInfo(questionnaire,sheet);
        
        //初始化信息
        List<QuestionnaireRecord> questionnaireRecordList = questionnaireRecordDao.findByQuestionnaireAndIsSubmittedTrue(questionnaire);
        Collection<Object> questions = questionService.getQuestions(questionnaire);
        List<RecordResponse> recordResponses = loadRecord(questionnaireRecordList,questions);
        
        
        writeQuestions(sheet,sheet.createRow(2),questions);

        int i = 3;
        int number = 0;
        for (RecordResponse response : recordResponses) {
            writeRecord(sheet.createRow(i++),response,++number);
        }
        
        return book;
    }
    
    //加载每个人的记录
    public List<RecordResponse> loadRecord(List<QuestionnaireRecord> questionnaireRecordList,Collection<Object> questions){
        List<RecordResponse> recordResponses  =new LinkedList<>();
        //加载每个人的信息
        for (QuestionnaireRecord questionnaireRecord : questionnaireRecordList) {
            RecordResponse response = new RecordResponse(questionnaireRecord);
            recordResponses.add(response);
            List<QuestionRecordAnalyseResponse> questionRecordAnalyseResponseList = new LinkedList<>();
            for (Object o: questions){
                if(o instanceof SingleChoiceQuestion){
                    SingleChoiceQuestion question = (SingleChoiceQuestion)o;
                    //设置题目信息
                    QuestionRecordAnalyseResponse questionRecordAnalyseResponse = new QuestionRecordAnalyseResponse(question);

                    //拿到对应题目记录
                    SingleChoiceQuestionRecord questionRecord = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(questionnaireRecord,question).get();
                    if(questionRecord.getAnswer()==null){
                        questionRecordAnalyseResponse.setIsFinish(false);
                    }
                    //获取答案
                    questionRecordAnalyseResponse.getAnswers().add(questionRecord.getAnswer());
                    //加载选项
                    for (SingleChoiceOption option : question.getOptions()) {
                        OptionRecordAnalyseResponse optionRecordAnalyseResponse = new OptionRecordAnalyseResponse();
                        optionRecordAnalyseResponse.setText(option.getText());
                        optionRecordAnalyseResponse.setDescription(option.getDescription());
                        optionRecordAnalyseResponse.setIsChoosed(questionRecordAnalyseResponse.getAnswers().contains(option.getId()));
                        optionRecordAnalyseResponse.setIsCorrect(option.getIsCorrect());

                        questionRecordAnalyseResponse.getOptions().add(optionRecordAnalyseResponse);
                    }
                    questionRecordAnalyseResponseList.add(questionRecordAnalyseResponse);
                    response.setQuestions(questionRecordAnalyseResponseList);
                }else if (o instanceof MultiChoiceQuestion){
                    MultiChoiceQuestion question = (MultiChoiceQuestion) o;
                    //设置题目信息
                    QuestionRecordAnalyseResponse questionRecordAnalyseResponse = new QuestionRecordAnalyseResponse(question);

                    //拿到对应题目记录
                    MultiChoiceQuestionRecord questionRecord = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(questionnaireRecord,question).get();
                    //获取答案

//                userQuestionRecord.getAnswers().add(questionRecord.getAnswer());
//                    List<MultiChoiceOptionRecord> optionRecords = multiChoiceOptionRecordDao.findByMultiChoiceQuestionRecord(questionRecord);
//                    for (MultiChoiceOptionRecord optionRecord : optionRecords) {
//                        userQuestionRecord.getAnswers().add(optionRecord.getMultiChoiceOption().getId());
//                    }
                    questionRecordAnalyseResponse.getAnswers().addAll(questionRecord.getAnswer());
                    if(questionRecordAnalyseResponse.getAnswers().isEmpty()){
                        questionRecordAnalyseResponse.setIsFinish(false);
                    }

                    //加载选项
                    for (MultiChoiceOption option : question.getOptions()) {
                        OptionRecordAnalyseResponse optionRecordAnalyseResponse = new OptionRecordAnalyseResponse();
                        optionRecordAnalyseResponse.setText(option.getText());
                        optionRecordAnalyseResponse.setDescription(option.getDescription());
                        optionRecordAnalyseResponse.setIsChoosed(questionRecordAnalyseResponse.getAnswers().contains(option.getId()));
                        optionRecordAnalyseResponse.setIsCorrect(option.getIsCorrect());
                        questionRecordAnalyseResponse.getOptions().add(optionRecordAnalyseResponse);
                    }
                    questionRecordAnalyseResponseList.add(questionRecordAnalyseResponse);
                    response.setQuestions(questionRecordAnalyseResponseList);
                }else if(o instanceof FillBlankQuestion){
                    FillBlankQuestion question = (FillBlankQuestion) o;
                    //设置题目信息
                    QuestionRecordAnalyseResponse questionRecordAnalyseResponse = new QuestionRecordAnalyseResponse(question);

                    //拿到对应题目记录
                    FillBlankQuestionRecord questionRecord = fillBlankQuestionRecordDao.findByQuestionnaireRecordAndFillBlankQuestion(questionnaireRecord,question).get();
                    if(questionRecord.getAnswer()==null){
                        questionRecordAnalyseResponse.setIsFinish(false);
                    }
                    //获取答案
                    questionRecordAnalyseResponse.setContent(questionRecord.getAnswer());

                    questionRecordAnalyseResponseList.add(questionRecordAnalyseResponse);
                    response.setQuestions(questionRecordAnalyseResponseList);
                }else if(o instanceof ScoreQuestion){
                    ScoreQuestion question = (ScoreQuestion) o;
                    //设置题目信息
                    QuestionRecordAnalyseResponse questionRecordAnalyseResponse = new QuestionRecordAnalyseResponse(question);

                    //拿到对应题目记录
                    ScoreQuestionRecord questionRecord = scoreQuestionRecordDao.findByQuestionnaireRecordAndScoreQuestion(questionnaireRecord,question).get();
                    if(questionRecord.getAnswer()==null){
                        questionRecordAnalyseResponse.setIsFinish(false);
                    }
                    //获取答案
                    questionRecordAnalyseResponse.getAnswers().add(questionRecord.getAnswer());
                    //加载选项
                    for (ScoreOption option : question.getOptions()) {
                        OptionRecordAnalyseResponse optionRecordAnalyseResponse = new OptionRecordAnalyseResponse();
                        optionRecordAnalyseResponse.setText(option.getText());
//                    userOptionResponse.setDescription(option.getDescription());
                        optionRecordAnalyseResponse.setIsChoosed(questionRecordAnalyseResponse.getAnswers().contains(option.getId()));
                        questionRecordAnalyseResponse.getOptions().add(optionRecordAnalyseResponse);
                    }
                    questionRecordAnalyseResponseList.add(questionRecordAnalyseResponse);
                    response.setQuestions(questionRecordAnalyseResponseList);
                }
            }
        }
        
        return recordResponses;
    }
    
    //写表头
    public void writeQuestionnaireInfo(Questionnaire questionnaire, Sheet sheet ){
        int i=0;
        int j=0;
        //第一行
        Row row = sheet.createRow(i++);
        Cell cell = row.createCell(j++);
        cell.setCellValue("问卷标题");
        
        cell = row.createCell(j++);
        cell.setCellValue("创建日期");
        
        cell = row.createCell(j++);
        cell.setCellValue("最近发布日期");
        
        //第二行
        row = sheet.createRow(i++);
        j=0;
        cell = row.createCell(j++);
        cell.setCellValue(questionnaire.getTitle());
        
        cell = row.createCell(j++);
        cell.setCellValue(format.format(questionnaire.getCreatedDate()));
        
        
        cell = row.createCell(j++);
        cell.setCellValue(format.format(questionnaire.getLatestPostDate()));
    }
    //写问题表头
    public void writeQuestions(Sheet sheet,Row row,Collection<Object> questions){
        int questionNum = 1;
        int i = 0;
        Cell cell = null;
        
        cell =row.createCell(i++);
        cell.setCellValue("序号");
//        sheet.autoSizeColumn(i-1);
        
        cell = row.createCell(i++);
        cell.setCellValue("提交日期");
//        sheet.autoSizeColumn(i-1);
        
        cell = row.createCell(i++);
        cell.setCellValue("所用时间");
//        sheet.autoSizeColumn(i-1);

        cell = row.createCell(i++);
        cell.setCellValue("IP地址");
//        sheet.autoSizeColumn(i-1);

        cell = row.createCell(i++);
        cell.setCellValue("IP所在地");
//        sheet.autoSizeColumn(i-1);
        
        cell = row.createCell(i++);
        cell.setCellValue("分数");
//        sheet.autoSizeColumn(i-1);
        
        for (Object o : questions) {
            if(o instanceof SingleChoiceQuestion){
                SingleChoiceQuestion question = (SingleChoiceQuestion)o;
                
                for (SingleChoiceOption option : question.getOptions()) {
                    cell = row.createCell(i++);
                    cell.setCellValue(questionNum+"、("+option.getText()+")");
//                    sheet.autoSizeColumn(i-1);
                }
            }else if(o instanceof MultiChoiceQuestion){
                MultiChoiceQuestion question = (MultiChoiceQuestion)o;
                for (MultiChoiceOption option : question.getOptions()) {
                    cell = row.createCell(i++);
                    cell.setCellValue(questionNum+"、["+option.getText()+"]");
//                    sheet.autoSizeColumn(i-1);
                }

            }else if(o instanceof FillBlankQuestion){
                FillBlankQuestion question = (FillBlankQuestion)o;
                cell = row.createCell(i++);
                cell.setCellValue(questionNum+"、"+question.getTitle());
//                sheet.autoSizeColumn(i-1);
            }else if(o instanceof ScoreQuestion){
                ScoreQuestion question = (ScoreQuestion) o;
                for (ScoreOption option : question.getOptions()) {
                    cell = row.createCell(i++);
                    cell.setCellValue(questionNum+"、<"+option.getText()+":"+option.getScore()+">");
//                    sheet.autoSizeColumn(i-1);
                }
            }
            
            questionNum++;
            
        }
    }
    
    
    public void writeRecord(Row row, RecordResponse recordResponse,int number){
        int i = 0;
        //序号
        Cell cell = row.createCell(i++);
        cell.setCellValue(number);
        
        //提交日期
        cell = row.createCell(i++);
        cell.setCellValue(format.format(recordResponse.getDate()));

        //耗时
        cell = row.createCell(i++);
        cell.setCellValue(recordResponse.getCompleteTime());

        //ip
        cell = row.createCell(i++);
        cell.setCellValue(recordResponse.getIp());

        //ip地址
        cell = row.createCell(i++);
        cell.setCellValue(recordResponse.getProvince()+recordResponse.getCity());

        cell = row.createCell(i++);
        cell.setCellValue(recordResponse.getGrade());
        
        for (QuestionRecordAnalyseResponse question : recordResponse.getQuestions()) {
            
            if(question.getType().equals("completion")){
                cell = row.createCell(i++);
                cell.setCellValue(question.getContent());
                
            }
            for (OptionRecordAnalyseResponse option : question.getOptions()) {
                cell = row.createCell(i++);
                cell.setCellValue(option.getIsChoosed()?"1":"0");
            }
        }
        
    }
    
    public int getHeadNum(){
        return 2;
    }
}
